//
//  SingleView.m
//  商城
//
//  Created by runone02 on 2018/8/30.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "SingleView.h"

@interface SingleView ()

/** 标题*/
@property (nonatomic,strong) UILabel *label;
/** 图片*/
@property (nonatomic,strong) UIImageView *imageView;

@end

@implementation SingleView


- (instancetype)initWithFrame:(CGRect)frame image:(NSString *)image title:(NSString *)title{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.title = title;
        self.imageTitel = image;
        [self initWithLabel];
    }
    return  self;
}


- (void)initWithLabel{
    UIImageView *image = [[UIImageView alloc]init];
//    [image setImage:IMAGE(@"addMatters")];
    [image setImage:IMAGE(_imageTitel)];
    [self addSubview:image];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(5);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(40);
    }];
    _imageView = image;
    
    UILabel *label = [[UILabel alloc] init];
    label.text = _title;
    label.userInteractionEnabled = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = TITLE_FONT(20);
    label.textColor = [UIColor grayColor];
    [self addSubview:label];
    _label = label;
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(image.mas_bottom).with.offset(2);
        make.left.mas_equalTo(self).with.offset(5);
        make.right.mas_equalTo(self).with.offset(-5);
        make.bottom.mas_equalTo(self).with.offset(-5);
    }];
    //长按手势
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(viewLongPressGesture:)];
    [self addGestureRecognizer:longGesture];
    
    //轻拍手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewtapGesture:)];
    [self addGestureRecognizer:tapGesture];
    
}

#pragma 长按手势
- (void)viewLongPressGesture:(UILongPressGestureRecognizer *)gesture{
    switch (gesture.state) {
            //移动前
        case UIGestureRecognizerStateBegan:
            if ([self.delegate respondsToSelector:@selector(endMoveViewAction:)]) {
                _label.textColor = [UIColor redColor];
                [self.delegate beginMoveAction:self.tagid];
            }
            break;
            //移动中
        case UIGestureRecognizerStateChanged:
            
            if ([self.delegate respondsToSelector:@selector(moveViewAction:gesture:)]) {
                
                [self.delegate moveViewAction:self.tagid gesture:gesture];
            }
            break;
            //移动后
        case UIGestureRecognizerStateEnded:
            
            if ([self.delegate respondsToSelector:@selector(endMoveViewAction:)]) {
                _label.textColor = [UIColor grayColor];
                [self.delegate endMoveViewAction:self.tagid];
            }
            break;
            
        default:
            break;
    }
}

#pragma 轻拍手势
- (void)viewtapGesture:(UITapGestureRecognizer *)gesture{
    if ([self.delegate respondsToSelector:@selector(clickOneViewReturnTitle:)]) {
        [self.delegate clickOneViewReturnTitle:_title];
    }
}


@end
